package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.dto.SetmealDTO;
import com.tm.entity.DishEntity;
import com.tm.entity.SetmealEntity;
import com.tm.service.SetmealService;
import com.tm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangp
 * @create 2023-04-06 15:59
 */
@RestController
@RequestMapping("setmeal")
@CrossOrigin
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("page")
    public Result getSetmealPage(@RequestParam(required = false,defaultValue = "1") Integer page,
                                 @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                 SetmealDTO setmealDTO){

        Page<SetmealEntity> page1 = setmealService.pageList(page,pageSize,setmealDTO);
        return Result.success(page1);
    }

    @PostMapping
    public Result addSetmeal(@RequestBody SetmealEntity setmealEntity){
        Boolean flag = setmealService.addSetmeal(setmealEntity);
        return Result.success(flag);
    }

    @GetMapping("/{id}")
    public Result querySetmealById(@PathVariable("id") Long id){
        SetmealEntity setmealEntity = setmealService.getById(id);
        return Result.success(setmealEntity);
    }

    @DeleteMapping
    public Result batchDelete(@RequestParam("ids") List<Long> ids){
        Boolean flag = setmealService.removeByIds(ids);
        return Result.success(flag);
    }

    @PutMapping
    public Result editDish(@RequestBody SetmealEntity setmealEntity ){
        UpdateWrapper<SetmealEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",setmealEntity.getId());
        Boolean flag = setmealService.update(setmealEntity,updateWrapper);
        return Result.success(flag);
    }

}
